package org.wxd.sign.aop;

import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.wxd.sign.utils.SignUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class SignHttpInputMessage implements HttpInputMessage {

    private InputStream body;
    private HttpHeaders headers;
    private Map<String,String> codeSignMap = new HashMap<>();

    public SignHttpInputMessage(HttpInputMessage httpInputMessage,MethodParameter methodParameter) throws IOException {
        String json = IOUtils.toString(httpInputMessage.getBody(),"UTF-8");
        body = IOUtils.toInputStream(json);
        headers = httpInputMessage.getHeaders();

    }

    @Override
    public InputStream getBody() throws IOException {
        return this.body;
    }

    @Override
    public HttpHeaders getHeaders() {
        return this.headers;
    }

    public boolean isSignError() {
        return SignUtils.isSignError(codeSignMap);
    }
}
